#include "Memory.h"
#include "MemAllocator.h"
FrameEntry frame_table[PHY_PAGE_NUM];
unsigned char PMem[PHY_MEM_SIZE];
void init_PMem(){
    for (int i=0;i<PHY_PAGE_NUM;++i){
        frame_table[i].pid=-1;
        frame_table[i].refSrc = (void *)0;
    }
}
PageTable *init_pageTable(){
    PageTable *pt = malloc(sizeof(PageTable));
    for (int i=0;i<VIR_PAGE_NUM;++i){
        pt->e[i].v = 0;
        pt->e[i].d = 0;
    }
    return pt;
}
void *destroy_pageTable(PageTable *pt){
    free(pt);
}
VPI *new_frame_refSrc(int pid, unsigned vpn, VPI *next){
    VPI *res = malloc(sizeof(VPI));
    res->pid = pid;
    res->VPN = vpn;
    res->next = next;
    return res;
}
VPI pop_frame_refSrc(VPI **phead){
    VPI res = **phead;
    VPI *next = (*phead)->next;
    free(*phead);
    *phead = next;
    return res;
}
void del_frame_refSrc(VPI *head){
    while (head){
        VPI *next = head->next;
        free(head);
        head = next;
    }
}
inline static int equal_frame(int pid, unsigned VPN, VPI *t){
    return t->pid==pid && t->VPN==VPN;
}
void del_specific_frame_refSrc(VPI **phead, int pid, unsigned VPN){
    if (equal_frame(pid,VPN,*phead)){
        pop_frame_refSrc(phead);
    }else{
        VPI *p = *phead;
        while (p->next){
            if (equal_frame(pid,VPN,p->next)){
                VPI *target = p->next;
                p->next = target->next;
                free(target);
                break;
            }
            p = p->next;
        }
    }
}